rename-item
Procedural macro to rename items on declaration, according to different case styles.
This crate provides the [macro@rename
] attribute-like macro that changes the name of the item it
is applied to. In order to refer to this changed name, this crate provides the [renamed!
]
function-like macro.
Both macros accept the same four named arguments for specifying the generated name:
-
The
name
argument specifies the individual components the generated name will be made up of. It can be a single string literal, as inname = "foo"
, or a list of string literals or identifiers, as inname("foo", bar)
. The components are split further into individual words:name("fooBar")
yields the same result asname("foo", "bar")
. This split is done using the [heck
] crate and follows its definition of words boundaries. Thename
argument is always required. -
The
case
argument specifies the case style the generated name will follow. It has to be a string literal, as incase = "snake"
. This case conversion is performed on the words extracted from thename
argument; theprefix
andsuffix
arguments explained below are not affected by the case conversion. Refer to the table below for a list of supported case styles. Thecase
argument is always required for the [renamed!
] macro; for the [macro@rename
] macro a default case style can be inferred from the annotated item's type. -
The
prefix
argument specifies an additional string literal that is prepended to the generated name. This is done after case conversion, so that the prefix string remains fixed independent of the target case style. Theprefix
argument is optional and defaults to the empty string. -
The
suffix
argument specifies an additional string literal that is appended to the generated name. This is done after case conversion, so that the suffix string remains fixed independent of the target case style. Thesuffix
argument is optional and defaults to the empty string.
The following case styles are supported:
Case style | case = |
Example |
---|---|---|
Upper camel case | "upper_camel" |
FooBar |
Lower camel case | "lower_camel" |
fooBar |
Snake case | "snake" |
foo_bar |
Shouty snake case | "shouty_snake" |
FOO_BAR |
This crate is mainly useful when writing declarative macros, as declarative macros cannot create new identifiers. Additionally, this crate allows declarative macros to adapt a given name to various case styles, useful when the macro defines different kinds of items.
Examples
use ;
// This function has the name `_return_five`
assert_eq!;
// This obtains the name of the function using the macro arguments from above, and calls it
let five = renamed!;
assert_eq!;
Here's a concrete example, a declarative macro that defines setters for struct members:
;
}
struct_with_setters!
let mut h = Highscore ;
h.set_score;
assert_eq!;
License
Licensed under either of MIT License or Apache License, Version 2.0 at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.